// featurePackage/pages/giftMain/giftMain.js
import {
    buyCardRecord,
    bindCardRecord,
    cardConsumeRecord,//礼品卡消费记录
} from "../../../service/jb_gift_service"
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        giftBgImg: "https://images.jiabao19.com/common/lllllll.png",
        menuTop: app.globalData.menuButton.top,
        menuHeight: app.globalData.menuButton.height,
        listType: 0, // 0:消费记录 1：购卡记录
        dataList: [
            // { type: 1 }, { type: 2 }, { type: 2 }, { type: 1 }
        ],
        tabs: [
            { id: '1', title: '消费记录', loaded: false },
            { id: '2', title: '绑卡记录', loaded: false },
            { id: '2', title: '购卡记录', loaded: false },
        ],
        emptyDes: "暂无消费记录",
        isShowLoading: false,//加载状态出现
        isBottomShow: false,//是否没有更多数据了
        page: 1,
        limit: 20,
        refreshFlag: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        if (options.listType) {
            this.setData({
                listType: options.listType
            })
            this.queryBuyCardRecord('refresh');
        } else {
            this.queryCardConsumeRecord('refresh');
        }
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    topCategoryChange(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            listType: index,
            emptyDes: index == 0 ? '暂无消费记录' : (index == 1 ? '暂无绑卡记录' : '暂无购卡记录')
        })
        this.setData({
            page: 1,
            isBottomShow: false,
            dataList: [],
        })
        if (index == 0) {
            //消费记录
            this.queryCardConsumeRecord('refresh')
        } else if (index == 1) {
            //绑卡记录
            this.queryBindCardRecord('refresh');
        } else {
            //购卡记录
            this.queryBuyCardRecord('refresh');
        }

    },


    // 刷新数据
    handleRefresh() {
        this.setData({
            page: 1,
            isBottomShow: false,
            refreshFlag: true,
        })
        if (this.data.listType == 0) {
            //消费记录
            this.queryCardConsumeRecord('refresh')
        } else if (this.data.listType == 1) {
            //绑卡记录
            this.queryBindCardRecord('refresh');
        } else {
            //购卡记录
            this.queryBuyCardRecord('refresh');
        }
    },
    handleLoadmore() {
        if (this.data.isBottomShow) {
            return;
        }
        this.setData({
            isShowLoading: true,
            page: this.data.page + 1
        })
        if (this.data.listType == 0) {
            //消费记录
            this.queryCardConsumeRecord('')
        } else if (this.data.listType == 1) {
            //绑卡记录
            this.queryBindCardRecord('');
        } else {
            //购卡记录
            this.queryBuyCardRecord('');
        }
    },


    //查询购卡记录
    queryBuyCardRecord(type) {
        wx.showLoading();
        buyCardRecord().then(res => {
            wx.hideLoading();
            if (res.code == 200 && res.data) {
                let arr = (type == 'refresh' ? [] : this.data.dataList || []);
                arr = arr.concat(res.data.list || [])
                this.setData({
                    dataList: arr,
                    refreshFlag: false,
                    isShowLoading: false,
                    isBottomShow: res.data.totalPage == this.data.page ? true : false,
                })
            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '查询失败',
                icon: "none"
            })
            if (this.data.page != 1) {
                this.setData({
                    page: this.data.page - 1
                })
            }
            this.setData({
                refreshFlag: false,
            })
            console.error('JJLog:buyCardRecord', err);
        })
    },
    //查询绑卡记录
    queryBindCardRecord(type) {
        wx.showLoading();
        bindCardRecord().then(res => {
            wx.hideLoading();
            if (res.code == 200 && res.data) {
                let arr = (type == 'refresh' ? [] : this.data.dataList || []);
                arr = arr.concat(res.data.list || [])
                this.setData({
                    dataList: arr,
                    refreshFlag: false,
                    isShowLoading: false,
                    isBottomShow: res.data.totalPage == this.data.page ? true : false,
                })
            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '查询失败',
                icon: "none"
            })
            if (this.data.page != 1) {
                this.setData({
                    page: this.data.page - 1
                })
            }
            this.setData({
                refreshFlag: false,
            })
            console.error('JJLog:bindCardRecord', err);
        })
    },
    //查询消费记录
    queryCardConsumeRecord(type) {
        wx.showLoading();
        cardConsumeRecord(this.data.page, this.data.limit).then(res => {
            wx.hideLoading();
            if (res.code == 200 && res.data) {
                let arr = (type == 'refresh' ? [] : this.data.dataList || []);
                arr = arr.concat(res.data.list || [])
                this.setData({
                    dataList: arr,
                    refreshFlag: false,
                    isShowLoading: false,
                    isBottomShow: res.data.totalPage == this.data.page ? true : false,
                })
            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '查询失败',
                icon: "none"
            })
            if (this.data.page != 1) {
                this.setData({
                    page: this.data.page - 1
                })
            }
            this.setData({
                refreshFlag: false,
            })
            console.error('JJLog:cardConsumeRecord', err);
        })
    },



})